/*
https://leetcode-cn.com/problems/find-peak-element/solution/xun-zhao-feng-zhi-by-leetcode-solution-96sj/
 */
public class Solution162 {
    public int findPeakElement(int[] nums) {
        if (nums.length==1) return 0;
        if (nums[0]>nums[1]){
            return 0;
        }
        if (nums[nums.length-1]>nums[nums.length-2]){
            return nums.length-1;
        }
        int l=1,r=nums.length-2,mid=(l+r)/2;
        while ((l <= r)) {
            if (nums[mid]>nums[mid+1]&&nums[mid]>nums[mid-1]){
                return mid;
            }
            if (nums[mid]<nums[mid-1]){
                r=mid-1;
            }else if(nums[mid]<nums[mid+1]){
                l=mid+1;
            }
            mid=(l+r)/2;
        }
        return 0;
    }

    public static void main(String[] args) {
        System.out.println(new Solution162().findPeakElement(new int[]{1,2,3,1}));
    }
}
